// 导入express
const express = require("express");
// 导入json
const { singers } = require("./JSON/singer.json");
console.log(singers);

// 创建应用对象
const app = express();

// 解决跨域
app.all("*", (req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  next();
});

// 创建路由
app.get("/singer/:id.html", (req, res) => {
  // 获取路由参数
  let { id } = req.params; // 对象解构赋值
  // 在数组中寻找对应id的数据
  let result = singers.find((item) => {
    if (item.id === Number(id)) {
      return true;
    }
  });
  // res.send(result); 发向前端
  // 服务器端渲染
  if (!result) {  // id 不存在时
    res.statusCode = 404;
    res.end("<h1>404 Not Found</h1>");
    return;
  }
  res.end(` 
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <h2>${result.singer_name}</h2>
    <img src='${result.singer_pic}' />
  </body>
  </html>`);
});

// 监听端口，启动服务
app.listen(3000, () => {
  console.log("服务启动成功,端口3000");
});
